//
//  BCAppVersionManager.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/18.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCAppVersionManager.h"

@interface BCAppVersionManager ()


@property (nonatomic, copy) NSString *configVersionCachePath;


/**
 *BConfigVersionResponse 上次请求成功存入文件的信息
 */
@property (nonatomic, strong) BConfigVersionResponse *lastConfigVersionResponse;

@end

@implementation BCAppVersionManager

static BCAppVersionManager *_appVersionManager = nil;
+ (instancetype)sharedManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_appVersionManager) {
            _appVersionManager = [[BCAppVersionManager alloc] init];
        }
    });
    return _appVersionManager;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.configVersionCachePath = [self createCachaVersionFile];
        //取出上一次数据
        [self initLoadData];
    }
    return self;
}
/**
 创建存放版本信息的文件
 */
- (NSString *)createCachaVersionFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, true);
    NSString *documentPath = paths[0];
    //创建目录
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *directoryFileName = @"F3APP";
    NSString *directoryPath = [documentPath stringByAppendingPathComponent:directoryFileName];
    if (![fm fileExistsAtPath:directoryPath]) {
        [fm createDirectoryAtPath:directoryPath withIntermediateDirectories:false attributes:nil error:nil];
    }
    //创建文件
    NSString *adsFileName = @"configVersion";
    NSString *directoryAds = [directoryPath stringByAppendingPathComponent:adsFileName];
    if (![fm fileExistsAtPath:directoryAds]) {
        [fm createFileAtPath:directoryAds contents:nil attributes:nil];
    }else {
        NSLog(@"已经创建!!!");
    }
    return directoryAds;
}

/**
 取出上一次的数据
 */
- (void)initLoadData {
    NSData *responseData = [NSData dataWithContentsOfFile:self.configVersionCachePath];
    if (responseData.length) {
        BConfigVersionResponse *response = [BConfigVersionResponse yy_modelWithJSON:responseData];
        self.lastConfigVersionResponse = response;
    }
}
/**
 request
 */
- (void)requestAppVersionDataComplete:(void (^)(BConfigVersionData *data,NSError *error))complete {
    BConfigVersionRequest *request = [BConfigVersionRequest new];
    WEAK_SELF
    [request didRequestSuccessed:^(id result, id page) {
        BConfigVersionResponse *resp = result;
        [wself handleConfigVersionDataResponse:resp];
        if (complete) {
            complete(resp.data,nil);
        }
    }];
    [request didRequestFailed:^(NSError *error) {
        if (complete) {
            complete(nil,error);
        }
    }];
    [request requestServer];
}
- (void)handleConfigVersionDataResponse:(BConfigVersionResponse *)resp {
    if (resp) {
        self.configVersionData = resp.data;
        //写入文件
        NSData *respData = [resp yy_modelToJSONData];
        WEAK_SELF
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [respData writeToFile:wself.configVersionCachePath atomically:true];
        });
    }
}

#pragma mark - get一些属性判断
//是否需要请求启动 来展示转换或正常界面
- (BOOL)needRequestLaunch {
    return false;
    if (self.lastConfigVersionResponse) {
        return false;
    }
    return true;
}
//是否显示正常UI
- (BOOL)showNormalUI {
    return true;
    if (self.configVersionData) {
        return !self.configVersionData.has_show;
    }
    if (self.lastConfigVersionResponse) {
        return !self.lastConfigVersionResponse.data.has_show;
    }
    return false;
}

@end
